package com.service.supply;

import com.common.DateUtils;
import com.common.DesUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.dao.supply.InfoMapper;
import com.dao.supply.LoginMapper;
import com.dao.supply.UserMapper;
import com.model.supply.SupplyInfoModel;
import com.model.supply.SupplyUserModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.UUID;

/**
 * @author lijiazhen
 * @date 2022-02-25 param description
 */
@Service
public class LoginService {
	@Autowired
	private LoginMapper loginMapper;

	@Autowired
	private InfoMapper infoMapper;

	@Autowired
	UserMapper userMapper;

	@Transactional
	public HttpResult supplierRegister(Map<String, Object> paramsMap) throws Exception {
		HttpResult httpResult = new HttpResult();
		String userName = (String) paramsMap.get("userName");// 用户姓名
		String loginName = (String) paramsMap.get("registerLoginName");// 注册账号
		String telePhoneNumber = (String) paramsMap.get("telePhoneNumber");// 联系方式
		String loginPassword = (String) paramsMap.get("loginPassword"); // 密码
		String registerSupplierName = (String) paramsMap.get("registerSupplierName");// 注册公司名称
		if (StringUtils.isBlank(loginName)) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("注册账号为空！");
			return httpResult;
		}
		if (StringUtils.isBlank(loginPassword)) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("密码为空，请设置密码！");
			return httpResult;
		}
		if (StringUtils.isBlank(registerSupplierName)) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("公司名称为空！");
			return httpResult;
		}
		if (StringUtils.isBlank(userName)) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("用户名不得为空！");
			return httpResult;
		}
		if (StringUtils.isBlank(telePhoneNumber)) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("联系方式不得为空！");
			return httpResult;
		}
		// 校验用户账号是否存在
		SupplyUserModel supplyUserInfo = loginMapper.querySupplierUserInfoByParams(loginName);
		if (supplyUserInfo != null) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("该用户账号已被注册！");
			return httpResult;
		}

		// 校验企业是否已被注册
		SupplyInfoModel supplyBaseInfo = loginMapper.querySupplierInfoByParams(paramsMap);
		if (supplyBaseInfo != null) {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("该公司已被注册！");
			return httpResult;
		}

		String userId = UUID.randomUUID().toString();
		String supplyInfoId = UUID.randomUUID().toString();
		// 新增供应商信息
		SupplyInfoModel supplyInfoAdd = new SupplyInfoModel();
		// 企业id
		supplyInfoAdd.setInfId(supplyInfoId);
		// 企业名称
		supplyInfoAdd.setInfName(registerSupplierName);
		// 联系人电话
		supplyInfoAdd.setInfMobile(telePhoneNumber);
		// 状态：新增时为待审核0
		supplyInfoAdd.setStatus(Global.NO + "");
		// 创建人：当前注册人账号
		supplyInfoAdd.setCreateName(loginName);
		// 创建人id
		supplyInfoAdd.setCreateId(userId);
		// 创建时间
		supplyInfoAdd.setCreateTime(DateUtils.getCurrentTime());
		supplyInfoAdd.setDelFlag(Global.NO + "");
		supplyInfoAdd.setInfMail((String) paramsMap.get("infMail"));
		int addTag = infoMapper.insert(supplyInfoAdd);
		if (addTag > 0) {
			// 新增用户信息（将注册用户设置为管理员）
			SupplyUserModel supplyUserModelAdd = new SupplyUserModel();
			// 用户ID
			supplyUserModelAdd.setUserId(userId);
			// 企业ID
			supplyUserModelAdd.setInfId(supplyInfoId);
			// 企业名称
			supplyUserModelAdd.setInforName(registerSupplierName);
			// 用户账号
			supplyUserModelAdd.setUserAccout(loginName);
			// 密码
			supplyUserModelAdd.setUserPwd(DesUtils.encrypt(loginPassword));
			// 姓名
			supplyUserModelAdd.setUserName(userName);
			// 新注册时，状态为待审核0
			supplyUserModelAdd.setStatus(Global.NO + "");
			// 是否为管理员 是--1；否--0
			supplyUserModelAdd.setUserIsAdmin("1");
			// 创建人：当前注册人账号
			supplyUserModelAdd.setCreateName(loginName);
			// 创建人id
			supplyUserModelAdd.setCreateId(userId);
			// 创建时间
			supplyUserModelAdd.setCreateTime(DateUtils.getCurrentTime());
			supplyUserModelAdd.setDelFlag(Global.NO + "");
			addTag = userMapper.insert(supplyUserModelAdd);
		}
		if (addTag > 0) {
			httpResult.setMsg("保存成功");
			httpResult.setCode(HttpStatus.SUCCESS_CODE);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("保存失败");
		}
		return httpResult;
	}

}
